Crate autocfg

source
Expand description

A Rust library for build scripts to automatically configure code based on compiler support. Code snippets are dynamically tested to see if the rustc will accept them, rather than hard-coding specific version support.

§Usage

Add this to your Cargo.toml:

[build-dependencies]
autocfg = "1"

Then use it in your build.rs script to detect compiler features. For example, to test for 128-bit integer support, it might look like:

extern crate autocfg;

fn main() {
    let ac = autocfg::new();
    ac.emit_has_type("i128");

    // (optional) We don't need to rerun for anything external.
    autocfg::rerun_path("build.rs");
}

If the type test succeeds, this will write a cargo:rustc-cfg=has_i128 line for Cargo, which translates to Rust arguments --cfg has_i128. Then in the rest of your Rust code, you can add #[cfg(has_i128)] conditions on code that should only be used when the compiler supports it.

§Caution

Many of the probing methods of AutoCfg document the particular template they use, subject to change. The inputs are not validated to make sure they are semantically correct for their expected use, so it’s possible to escape and inject something unintended. However, such abuse is unsupported and will not be considered when making changes to the templates.

Structs§

  • Helper to detect compiler features for cfg output in build scripts.
  • A common error type for the autocfg crate.

Functions§

  • Writes a config flag for rustc on standard out.
  • Indicates to rustc that a config flag should not generate an unexpected_cfgs warning
  • Creates a new AutoCfg instance.
  • Writes a line telling Cargo to rerun the build script if the environment variable var changes.
  • Writes a line telling Cargo to rerun the build script if path changes.